// poke_dlg_window_prop.cpp : implementation file
//

#include "stdafx.h"
#include "poke.h"
#include "ui/poke_dlg_window_prop.h"
#include "models/atom_interface/ai_window_info_fetcher.h"
#include "poke_framework.h"

// CPokeDlgWindowProp dialog

IMPLEMENT_DYNAMIC(CPokeDlgWindowProp, CDialog)

CPokeDlgWindowProp::CPokeDlgWindowProp(CWnd* pParent /*=NULL*/)
    : CDialog(CPokeDlgWindowProp::IDD, pParent)
{
    PokeFramework::get()->AddMCA(this);
}

CPokeDlgWindowProp::~CPokeDlgWindowProp()
{
    PokeFramework::get()->RemoveMCA(this);
}

BOOL CPokeDlgWindowProp::OnInitDialog()
{
    CDialog::OnInitDialog();

    m_oListWindowProp.SetExtendedStyle(m_oListWindowProp.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

    LVCOLUMN oColumn = {0};
    oColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
    oColumn.fmt = LVCFMT_LEFT;

    oColumn.pszText = _T("Atom?");
    oColumn.iSubItem = 0;
    oColumn.cx = 50;
    m_oListWindowProp.InsertColumn(0, &oColumn);

    oColumn.pszText = _T("Key");
    oColumn.iSubItem = 1;
    oColumn.cx = 270;
    m_oListWindowProp.InsertColumn(1, &oColumn);

    oColumn.pszText = _T("Value");
    oColumn.iSubItem = 2;
    oColumn.cx = 100;
    m_oListWindowProp.InsertColumn(2, &oColumn);

    return TRUE;
}

void CPokeDlgWindowProp::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_LIST_WINDOW_PROP, m_oListWindowProp);

    WindowProps oProps;
    Execute(PAN_WINDOW_INFO_FETCHER, PFI_GET_WINDOW_PROPS, &oProps, NULL);

    m_oListWindowProp.DeleteAllItems();

    int i = 0;
    CString strValue;
    std::map<WindowPropKey, HANDLE>::iterator oPropIt = oProps.m_vPropList.begin();
    while(oPropIt != oProps.m_vPropList.end()) {
        strValue.Format(_T("0x%08x"), oPropIt->second);

        m_oListWindowProp.InsertItem(i, _T(""));
        m_oListWindowProp.SetItemText(i, 0, oPropIt->first.m_bIsAtom ? _T("YES") : _T("NO"));
        m_oListWindowProp.SetItemText(i, 1, oPropIt->first.m_strPropKey);
        m_oListWindowProp.SetItemText(i, 2, strValue);
        ++i;
        ++oPropIt;
    }
}

bool CPokeDlgWindowProp::OnEvent(const M::AtomEndPoint &oSrcAtom, int nEventId, void *pParams)
{
    switch(nEventId) {
    case PEI_UPDATE_UI:
        return !!UpdateData(FALSE);
    }

    return true;
}

BEGIN_MESSAGE_MAP(CPokeDlgWindowProp, CDialog)
END_MESSAGE_MAP()

// CPokeDlgWindowProp message handlers
